#include <Rectangle.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

const float precision = 0.01;
const float maxLength = 100.00;

Rectangle::Rectangle (float width, float height) 
{
   m_width  = validateLength (width);
   m_height = validateLength(height);
}

bool Rectangle::setLength (float &destination, float length)
{
   bool valid = ((validateLength (length) - 0) >= precision);

   valid ? (destination = length) : 0;
   return valid;
}

bool Rectangle::setWidth  (float width)
{
   return setLength (m_width, width);
}

bool Rectangle::setHeight (float height)
{
   return setLength (m_height, height);
}

float Rectangle::getArea () const
{
   return validateResult (m_height * m_width);
}

float Rectangle::getPerimeter () const
{
   return validateResult (2 * (m_height + m_width));
}

float Rectangle::validateLength (float length)
{
   /* max value 100.00 */
   char buf[8];
   int point  = 0;

   if ((length > (maxLength + precision)) || (length < precision))
      return 0;

   sprintf (buf, "%.3f", length);
   
   /* locate the point inside float */ 
   while ((buf[point] != '.') && (point < 8))
      point++;

   /* validate only 2 chars after point */
   if ((point + 3) <= 7)
      buf[point + 3] = 0;

   return atof (buf);
}

float Rectangle::validateResult (float result) 
{
   char buf[9] = {0};

   sprintf (buf, "%.2f", result);
   return atof (buf);
}

